"""
When a socket is created, an identifier, called a port number, is assigned to it.
In summary, the sending process attaches to the packet a destination address, which
consists of the destination host’s IP address and the destination socket’s port number.
However, attaching the source address to the packet is typically not done by the UDP application code;
instead it is automatically done by the underlying operating system.
"""


from socket import *
# serverName = "hostname"
'''
Here, we provide a string containing either the IP address of the server (e.g., “128.138.32.126”) 
or the hostname of the server (e.g., “cis.poly.edu”)
'''

serverName = "192.168.124.17"
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_DGRAM)

while True:
    message = input("Input lowercase sentence:")
    clientSocket.sendto(message.encode(), (serverName, serverPort))
    modifiedMessage, serverAddress = clientSocket.recvfrom(2048)

# With the above line, when a packet arrives from the Internet at the client’s socket, the packet’s data is
# put into the variable modifiedMessage and the packet’s source address is put into the variable serverAddress.
# The program UDPClient doesn't actually need this server address information,
# since it already knows the server address from the outset;
# but this line of Python provides the server address nevertheless.
# The method recvfrom also takes the buffer size 2048 as input.

    print("Data from the UDP server:", modifiedMessage.decode())


clientSocket.close()
